﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using System.IO;
using University.Workshop.Model;

namespace University.Workshop.WinApp
{
    public partial class SearchProduct : Form
    {
        bool buySelected;
        Product product = new Product();
        ProductBuy buy = new ProductBuy();
        public SearchProduct()
        {
            InitializeComponent();
            LoadDataGridViewBuyProduct();
            buySelected = false;
            //IssuePermits();
        }

        //private void IssuePermits()
        //{
        //    //buttonModifyPayment.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        //}

        private void LoadDataGridViewBuyProduct()
        {
            dataGridViewSearchProduct.DataSource = ProductManager.LoadDataGridBuy();
        }

        private void textBoxSearchProduct_TextChanged(object sender, EventArgs e)
        {
            if (ValidateFields.SearchFieldValidation(textBoxSearchProduct.Text))
            {
                dataGridViewSearchProduct.DataSource = ProductManager.LoadDataGridViewSearchBuyProduct(textBoxSearchProduct.Text);
            }
            else
            {
                dataGridViewSearchProduct.DataSource = null;
            }
        }

        private void dataGridViewSearchProduct_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LoadDateBuyProduct();
            buySelected = true;
        }

        private void LoadDateBuyProduct()
        {       
            textBoxIdBuy.Text = dataGridViewSearchProduct.CurrentRow.Cells[0].Value.ToString();
            textBoxDateBuy.Text = dataGridViewSearchProduct.CurrentRow.Cells[1].Value.ToString();
            textBoxSupplier.Text = dataGridViewSearchProduct.CurrentRow.Cells[2].Value.ToString();
            textBoxStockOrder.Text = dataGridViewSearchProduct.CurrentRow.Cells[3].Value.ToString();
            textBoxUnitPrice.Text = dataGridViewSearchProduct.CurrentRow.Cells[4].Value.ToString();
            textBoxTotalPrice.Text = dataGridViewSearchProduct.CurrentRow.Cells[5].Value.ToString();
            textBoxDescription.Text = dataGridViewSearchProduct.CurrentRow.Cells[7].Value.ToString();
            DataTable loadProduct = ProductManager.LoadDataGridProduct(dataGridViewSearchProduct.CurrentRow.Cells[8].Value.ToString());
            textBoxCodeProduct.Text = loadProduct.Rows[0]["IdProduct"].ToString();
            textBoxGroupProduct.Text = loadProduct.Rows[0]["GroupProduct"].ToString();
            textBoxNameProduct.Text = loadProduct.Rows[0]["NameProduct"].ToString();
            textBoxStockCurrent.Text = loadProduct.Rows[0]["StockCurrent"].ToString();
            textBoxNameUser.Text = UserManager.LoadDataManager(dataGridViewSearchProduct.CurrentRow.Cells[9].Value.ToString());
            try
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", dataGridViewSearchProduct.CurrentRow.Cells[6].Value.ToString())));
                pictureBoxImageProduct.Image = picture;
            }
            catch (Exception)
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", "anonimos.jpg")));
                pictureBoxImageProduct.Image = picture;
            }
            buy.IdBuyProduct=int.Parse(textBoxIdBuy.Text);
            buy.DateBuy = textBoxDateBuy.Text;
            buy.Supplier = textBoxSupplier.Text;
            buy.StockOrder = int.Parse(textBoxStockOrder.Text);
            buy.UnitPrice = float.Parse(textBoxUnitPrice.Text);
            buy.TotalPrice = float.Parse(textBoxTotalPrice.Text);
            buy.ImageBuy = dataGridViewSearchProduct.CurrentRow.Cells[6].Value.ToString();
            buy.Description = textBoxDescription.Text;
            buy.CIManager = dataGridViewSearchProduct.CurrentRow.Cells[9].Value.ToString();
            product.IdProduct = int.Parse(textBoxCodeProduct.Text);
            product.GroupProduct = textBoxGroupProduct.Text;
            product.NameProduct = textBoxNameProduct.Text;
            product.StockCurrent = int.Parse(textBoxStockCurrent.Text);

        }

        private void SearchProduct_FormClosed(object sender, FormClosedEventArgs e)
        {
            Singleton.Instance.valid = true;
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            if (buySelected)
            {
                ModifyBuyProduct modifyBuy = new ModifyBuyProduct(product,buy);
                modifyBuy.ShowDialog();
                LoadDataGridViewBuyProduct();
            }
            else
            {
                MessageBox.Show("Error: You Have Select a Product");
            }
        }

    }
}
